package com.zelma.agent.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.zelma.agent.util.JwtUtil;
import com.zelma.common.utils.PageUtils;
import com.zelma.common.utils.R;
import io.jsonwebtoken.Claims;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundListOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import com.zelma.agent.entity.AgentEntity;
import com.zelma.agent.service.AgentService;

import javax.servlet.http.HttpServletRequest;


/**
 * 
 *
 * @author zelma
 * @email 1239115389@qq.com
 * @date 2022-01-06 16:48:57
 */
@RestController
@RequestMapping("agent/agent")
public class AgentController {
    @Autowired
    JwtUtil jwtUtil;

    @Autowired
    AgentService agentService;

    @Autowired
    RabbitTemplate rabbitTemplate;

    @Autowired
    StringRedisTemplate redisTemplate;


    //经纪人管理页面--wq
    @GetMapping("/list")
    public R list(@RequestParam Map<String, Object> params, HttpServletRequest request){
        String token = request.getHeader("token");
        if (token!=null ){
            Claims claims = jwtUtil.parseJWT(token);
            if (claims!=null &&claims.get("roles").equals("admin")){
                PageUtils page = agentService.queryPage(params);

                return R.ok().put("page", page);
            }
        }
        return R.error("无权限访问！！");
    }

    //经纪人登录接口--wq
    @PostMapping("/Login")
    public R login(@RequestBody AgentEntity agent,@RequestParam("authCode") String authCode){
        AgentEntity agentPhone = agentService.getOne(new QueryWrapper<AgentEntity>().eq("agent_phone", agent.getAgentPhone()));
        if (agentPhone != null && agent.getAgentName().equals(agentPhone.getAgentName()))
        {
            String s = redisTemplate.opsForValue().get("AgentAuthCode");
            if (authCode != null){
                if (authCode.equals(s)){
                    String token = jwtUtil.createJWT(agent.getAgentName(), agent.getAgentPhone(), "agent");
                    redisTemplate.opsForValue().set("name",agent.getAgentName());
                    redisTemplate.opsForValue().set("status","agent");
                    BoundListOperations<String, String> agents = redisTemplate.boundListOps("agents");
                    agents.leftPush(agent.getAgentName());
                    return R.ok("登陆成功").put("token",token);
                }
                return R.error(500,"验证码输入有误");
            }
            return R.error(404,"请输入验证码");
        }
        return R.error(404,"您输入的手机号不正确");
    }

    //经纪人发送验证码接口--wq
    @GetMapping("/authCode/{agentPhone}")
    public R authCode(@PathVariable("agentPhone") String agentPhone){
        AgentEntity phone = agentService.getOne(new QueryWrapper<AgentEntity>().eq("agent_phone", agentPhone));
        if (phone!=null){
            //点击发送验证码，使用MQ异步发送验证码
            rabbitTemplate.convertAndSend("1904",agentPhone);
            return R.ok();
        }
       return R.error(404,"该手机号未注册，请重新输入");
    }

    //经纪人页面编辑按钮接口--wq
    @PutMapping("/updateAgent")
    public R update(@RequestBody AgentEntity agentEntity){
        agentService.updateById(agentEntity);
        return R.ok();
    }

    //经纪人页面删除按钮接口--wq
    @DeleteMapping("/deleteAgent")
    public R delete(@RequestParam("agentId") String agentId){
        agentService.removeById(agentId);
        return R.ok();
    }
    //经纪人页面新建按钮接口--wq
    @PostMapping("/saveAgent")
    public R save(@RequestBody AgentEntity agentEntity){
        agentService.save(agentEntity);
        return R.ok();
    }

    //经纪人页面排行榜接口--wq
    @PostMapping("/RankingByName")
    public R RankingByName(@RequestBody AgentEntity agentEntity){
        agentService.RankingByName(agentEntity.getAgentName());
        return R.ok();
    }

}
